//
//  AccountAndPasswordViewController.m
//  AI
//
//  Created by WTTT on 2023/8/2.
//

#import "AccountAndPasswordViewController.h"
#import "ViewController.h"
#import <YYText.h>
@interface AccountAndPasswordViewController ()
@property (nonatomic , strong) UITextField * name;
@property (nonatomic , strong) UITextField * password;
@property (nonatomic , assign) BOOL isXieyi;
 
@end

@implementation AccountAndPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}
- (void)SQ_UIViewForSelfView{
    UIImageView * image =  [[UIImageView alloc]init];
     image.image = SQGetImage(@"背景1");
     image.contentMode = UIViewContentModeScaleAspectFill;
     [self.view addSubview:image];
     [image mas_makeConstraints:^(MASConstraintMaker *make) {
         make.edges.equalTo(self.view);
     }];
    
    UIImageView * logoimage =  [[UIImageView alloc]init];
    logoimage.image = SQGetImage(@"logo");
    logoimage.contentMode = UIViewContentModeScaleAspectFill;
     [self.view addSubview:logoimage];
     [logoimage mas_makeConstraints:^(MASConstraintMaker *make) {
         make.width.mas_equalTo(70);
         make.height.mas_equalTo(100);
         make.centerX.equalTo(self.view);
         make.top.equalTo(self.view).offset(120);
     }];
    [self.view addSubview:self.name];
    [self.view addSubview:self.password];
    [self.name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(SCREEN_WIDETH - 40);
        make.centerX.equalTo(self.view);
        make.top.equalTo(logoimage.mas_bottom).offset(110);
        make.height.mas_equalTo(50);
    }];
    
    [self.password mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(SCREEN_WIDETH - 40);
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.name.mas_bottom).offset(24);
        make.height.mas_equalTo(50);
    }];
   
    UILabel * label = [[UILabel alloc]init];
    label.text = SQNSLocalString(@"首次输入默认创建，请保管好您的账号密码");
    label.font = FONT_MEDIUM(12);
    label.textColor = SQColor(@"#B2B2B2");
    [self.view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(30);
        make.top.equalTo(self.password.mas_bottom).offset(16);
    }];
    
    HeaderView * btn = [[HeaderView alloc]init];
    btn.font = FONT_BOLD(16);
    btn.text = SQNSLocalString(@"登录");
    btn.layer.cornerRadius = 25;
    btn.headerImage.image = SQGetImage(@"按钮背景");
    [btn.btn addTarget:self action:@selector(SQ_buttonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.password.mas_bottom).offset(70);
        make.height.mas_equalTo(50);
        make.width.mas_equalTo(SCREEN_WIDETH-60);
        make.centerX.equalTo(self.view);
    }];
    
    WS(weakSelf);
    YYLabel * label3 = [[YYLabel alloc]initWithFrame:CGRectMake(SCREEN_WIDETH/2.f-100, SCREEN_HEIGHT- FootHeight-60, 260, 40)];
    label3.textVerticalAlignment = YYTextVerticalAlignmentCenter;
    label3.textAlignment = NSTextAlignmentCenter;
    label3.numberOfLines = 0;
    [self.view addSubview:label3];
    NSDictionary *attributes = @{NSFontAttributeName:FONT_MEDIUM(12) , NSForegroundColorAttributeName: SQHexColor(@"#7C7C7C", 0.8)};
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:SQNSLocalString(@"我已阅读并同意 《用户协议》、《隐私协议》.") attributes:attributes];
   
    //设置高亮色和点击事件
    [text yy_setTextHighlightRange:[[text string] rangeOfString:SQNSLocalString(@"《用户协议》")] color:SQColor(@"#FFFFFF") backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        CommonWebVcView *vc= [[CommonWebVcView alloc]init];
        vc.urlStr =  YHXY;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
    NSRange range1=[[text string]rangeOfString:SQNSLocalString(@"《用户协议》")];
    YYTextDecoration* deco=[YYTextDecoration decorationWithStyle:(YYTextLineStyleSingle) width:[NSNumber numberWithInt:0] color:[UIColor whiteColor]];
    [text yy_setTextUnderline:deco range:range1];
    
    //设置高亮色和点击事件
    [text yy_setTextHighlightRange:[[text string] rangeOfString:SQNSLocalString(@"《隐私协议》")] color:SQColor(@"#FFFFFF") backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        CommonWebVcView *vc= [[CommonWebVcView alloc]init];
        vc.urlStr =  YSTK;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
    NSRange range2=[[text string]rangeOfString:SQNSLocalString(@"《隐私协议》") ];
    YYTextDecoration* deco2=[YYTextDecoration decorationWithStyle:(YYTextLineStyleSingle) width:[NSNumber numberWithInt:0] color:[UIColor whiteColor]];
    [text yy_setTextUnderline:deco2 range:range2];
    label3.attributedText = text;
    
    SQCustomButton * leftbtn = [[SQCustomButton alloc]initType:SQCustomButtonLeftImageType imageSize:CGSizeMake(15, 15) midmargin:6];
    leftbtn.image = SQGetImage(@"选择");
    [self.view addSubview:leftbtn];
    [leftbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(label3);
        make.height.mas_equalTo(40);
        make.right.equalTo(label3.mas_left);
        make.width.mas_equalTo(40);
    }];
    
    [leftbtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        weakSelf.isXieyi = !weakSelf.isXieyi;
        if (weakSelf.isXieyi) {
            button.image = SQGetImage(@"未选择");
        } else {
            button.image = SQGetImage(@"选择");
        }
    }];
    
}
-(void)SQ_buttonClick{
    if (![self.name.text isNotEmpty]) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"请输入账号")];
        return;
    }
    if (![self.password.text isNotEmpty]) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"请输入密码")];
        return;
    }
    if (self.isXieyi) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"请查看并勾选协议")];
        return;
    }
    if (self.name.text.length <6) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"账号最少6位数")];
        return;
    }
    if (self.password.text.length <6) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"密码最少6位数")];
        return;
    }
    
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:self.name.text forKey:@"account"];
    [dic setObject:self.password.text forKey:@"passwd"];
    [YJProgressHUD SQ_showMsgWithoutView:@""];
    [HttpTool post:Url3 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        NSDictionary * data = [[NSDictionary alloc]initWithDictionary:httpModel.data];
        UserModel * model = [UserModel mj_objectWithKeyValues:data];
        [UserModel updateLocalUserModel:model];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[Manager SQ_getViewController]];
         self.view.window.rootViewController = nav;
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}


 
- (UITextField *)name{
    if (!_name) {
        _name = [[UITextField alloc]init];
        _name.attributedPlaceholder = [[NSAttributedString alloc] initWithString:SQNSLocalString(@"请输入6-15位英文和数字作为账号")  attributes:@{NSForegroundColorAttributeName : SQColor(@"#BDBDBD")}];
        _name.font =  FONT_REGULAR(16);
        _name.textColor = SQColor(@"#FFFFFF");
        _name.backgroundColor =[UIColor colorWithHexString:@"#6D628F" alpha:0.5];
        _name.layer.cornerRadius = 25;
        _name.textAlignment = NSTextAlignmentCenter;
        _name.layer.masksToBounds = NO;
        
    }
    return _name;
}

- (UITextField *)password{
    if (!_password) {
        _password = [[UITextField alloc]init];
        _password.attributedPlaceholder = [[NSAttributedString alloc] initWithString:SQNSLocalString(@"请输入6-11位英文和数字作为密码")  attributes:@{NSForegroundColorAttributeName : SQColor(@"#BDBDBD")}];
        _password.font = FONT_REGULAR(16);
        _password.textColor = SQColor(@"#FFFFFF");
        _password.secureTextEntry = YES;
        _password.backgroundColor = [UIColor colorWithHexString:@"#6D628F" alpha:0.5];
        _password.layer.cornerRadius = 25;
        _password.layer.masksToBounds = NO;
        // 阴影颜色
        _password.textAlignment = NSTextAlignmentCenter;
        
    }
    return _password;
}

@end
